package com.rabbiter.library.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rabbiter.library.annotation.Auth;
import com.rabbiter.library.entity.SysReader;
import com.rabbiter.library.utils.ResultUtils;
import com.rabbiter.library.utils.ResultVo;
import com.rabbiter.library.service.SysRoleService;
import com.rabbiter.library.service.RoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private RoleMenuService roleMenuService;

    @ApiOperation(value = "添加角色", notes = "添加新的系统角色")
    @Auth
    @PostMapping
    public ResultVo addRole(@RequestBody SysReader.SysRole role) {
        if (role.getRoleType().equals("2")) {
            QueryWrapper<SysReader.SysRole> query = new QueryWrapper<>();
            query.lambda().eq(SysReader.SysRole::getRoleType, "2");
            SysReader.SysRole one = sysRoleService.getOne(query);
            if (one != null) {
                return ResultUtils.error("读者角色已经存在!");
            }
        }
        role.setCreateTime(new Date());
        boolean save = sysRoleService.save(role);
        if (save) {
            return ResultUtils.success("新增角色成功!");
        }
        return ResultUtils.error("新增角色失败!");
    }

    @ApiOperation(value = "编辑角色", notes = "更新系统角色信息")
    @Auth
    @PutMapping
    public ResultVo editRole(@RequestBody SysReader.SysRole role) {
        if (role.getRoleType().equals("2")) {
            QueryWrapper<SysReader.SysRole> query = new QueryWrapper<>();
            query.lambda().eq(SysReader.SysRole::getRoleType, "2");
            SysReader.SysRole one = sysRoleService.getOne(query);
            if (one != null && role.getRoleId() != one.getRoleId()) {
                return ResultUtils.error("读者角色已经存在!");
            }
        }
        role.setCreateTime(new Date());
        boolean save = sysRoleService.updateById(role);
        if (save) {
            return ResultUtils.success("编辑角色成功!");
        }
        return ResultUtils.error("编辑角色失败!");
    }

    @ApiOperation(value = "删除角色", notes = "根据ID删除系统角色")
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataTypeClass = Long.class, paramType = "path")
    @Auth
    @DeleteMapping("/{roleId}")
    public ResultVo deleteRole(@PathVariable("roleId") Long roleId) {
        boolean remove = sysRoleService.removeById(roleId);
        if (remove) {
            return ResultUtils.success("删除角色成功!");
        }
        return ResultUtils.error("删除角色失败!");
    }

    @ApiOperation(value = "获取角色列表", notes = "分页查询系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataTypeClass = String.class)
    })
    @Auth
    @GetMapping("/list")
    public ResultVo getList(SysReader.RoleParm parm) {
        IPage<SysReader.SysRole> list = sysRoleService.list(parm);
        return ResultUtils.success("查询成功!", list);
    }

    @ApiOperation(value = "获取角色权限", notes = "获取角色已分配的菜单权限")
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataTypeClass = Long.class)
    @Auth
    @GetMapping("/getAssingShow")
    public ResultVo getAssingShow(SysReader.AssignParm parm) {
        SysReader.AssignVo show = sysRoleService.getAssignShow(parm);
        return ResultUtils.success("查询成功", show);
    }

    @ApiOperation(value = "分配权限", notes = "为角色分配菜单权限")
    @Auth
    @PostMapping("/assignSave")
    public ResultVo assignSave(@RequestBody SysReader.SaveAssign parm) {
        roleMenuService.assignSave(parm.getRoleId(), parm.getList());
        return ResultUtils.success("分配成功!");
    }
}
